package com.fire.user.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fire.core.base.controller.BaseController;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.DateUtil;
import com.fire.core.util.HttpUtils;
import com.fire.user.constants.ErrorCodeConstants;
import com.fire.user.model.FireProprietorUserModel;
import com.fire.user.model.SiteModel;
import com.fire.user.request.WchatPushxReq;
import com.fire.user.service.IFireProprietorUserService;
import com.fire.user.service.ISiteService;
import com.google.gson.JsonElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 处理通知
 *
 * @author raoyuxing
 * @Date 2018-04-24
 */
@RestController
public class UMSController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(UMSController.class);

	@Autowired
	ISiteService siteService;
	@Autowired
	IFireProprietorUserService fireProprietorUserService;
	@Value("${server.port}")
	String port;
	@Value("${server.servlet.context-path}")
	String path;

	@PostMapping("/alertNotice")
	public void alertNotice(@RequestBody String json) {
		LOGGER.info(json);
		Map map = JSON.parseObject(json, Map.class);
		//获取需要推送的关键字
		String siteName = (String) map.get("meta.siteName");
		String assetName = (String) map.get("meta.assetName");
		String location = (String) map.get("meta.location");
		String categoryname = (String) map.get("meta.categoryname");
		String alertMessage = (String) map.get("alertMessage");
		String eventDate = (String) map.get("eventDate");
		WchatPushxReq wchatPushxReq = new WchatPushxReq();
		wchatPushxReq.setFirst(siteName);
		wchatPushxReq.setKeyword1(alertMessage);
		wchatPushxReq.setKeyword2(location);
		wchatPushxReq.setKeyword3(assetName);
		wchatPushxReq.setKeyword4(categoryname);
		wchatPushxReq.setKeyword5(UTCStringtODefaultString(eventDate));
		//获取需要接收推送的用户信息
		String siteToken = (String) map.get("siteToken");
		SiteModel site = siteService.selectOne(new EntityWrapper<SiteModel>().eq("sitetoken", siteToken));
		List<FireProprietorUserModel> proprietorList = fireProprietorUserService.selectList(new EntityWrapper<FireProprietorUserModel>().eq("proprietor", site.getProprietor()));
		for (FireProprietorUserModel model : proprietorList) {
			wchatPushxReq.setUserid(model.getUserid());
			String url = "http://127.0.0.1:" + port + path + "/pushx";
			String response = HttpUtils.doPost(url, JSON.toJSONString(wchatPushxReq));
			AjaxResult pushRes = JSON.parseObject(response, AjaxResult.class);
			String push_string = "";
			if (ErrorCodeConstants.SUCCESS.equals(pushRes.getStatus())) {
				push_string = "成功";
			} else {
				push_string = "失败";
			}
			LOGGER.info("id为" + model.getUserid() + "的用户推送" + push_string);
		}

	}

	public String UTCStringtODefaultString(String date) {
		date = date.replace("Z", " UTC");//注意是空格+UTC
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
		Date d = null;
		try {
			d = format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return DateUtil.getTime(d);
	}
}
